﻿using LayuiMvc.DataEntity.DataEnum;
using LayuiMvc.DataEntity.IdentityEntity;
using LayuiMvc.DataEntity.IdentityStore;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LayuiMvc.DataEntity
{
    public class DBInitializer:DropCreateDatabaseIfModelChanges<EFContext>
    {
        protected override void Seed(EFContext context)
        {
            //角色管理器
            RoleManager<IdentityRoleEntity, int> roleManager =
                new RoleManager<IdentityRoleEntity, int>(new IdentityRoleStore(context));
            //用户管理器
            UserManager<IdentityUserEntity,int> userManager=new UserManager<IdentityUserEntity, int>(new IdentityUserStore(context));

            IdentityRoleEntity adminRole = new IdentityRoleEntity()
            {
                Name = "Admin",
                Description = "管理员",
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now
            };

            IdentityRoleEntity commonStudentRole = new IdentityRoleEntity()
            {
                Name = "CommonStudent",
                Description = "普通学员",
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now
            };

            IdentityRoleEntity vipStudentRole = new IdentityRoleEntity()
            {
                Name = "VIPStudent",
                Description = "VIP学员",
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now
            };
            roleManager.Create(adminRole);
            roleManager.Create(commonStudentRole);
            roleManager.Create(vipStudentRole);

            IdentityUserEntity adminUser = new IdentityUserEntity()
            {
                UserName = "admin",
                NickName = "Robin老师",
                Sex = 10,
                IsApprove=true,
                ApproveMsg="项目开发者",
                UserIdentity=UserIdentityEnum.Admin,
                RegisterTime = DateTime.Now,
                PhoneNumber = "15815821551",
                Email = "15815821551@139.com",
                PhoneNumberConfirmed = true
            };

            userManager.Create(adminUser, "123456");
            userManager.AddToRole(adminUser.Id, adminRole.Name);

            base.Seed(context);
        }
    }
}
